﻿/******************************************************************
 *                  Dipan.SanGuo Tent Utils                       *
 *                                                                *
 * File Name: Dipan.SanGuo.Tent.js                                *
 * Written by: yangc (sheepchang@163.com)                         *
 * Important: to use this script don't                            *
 * remove these comments                                          *
 * Version 1.0 (MSIE 6.0 above,Firefox2.0,Netscape.)              *
 * Created Date: 2008-06-30                                       *
 ******************************************************************/

/*营寨*/
Dipan.SanGuo.Tent =
{
    /**
     * 营寨初始化

     */
    Initialize: function()
    {
        //Dipan.SanGuo.Tent.GetBuilding();
        //Dipan.SanGuo.Tent.GetCityInfo();
       // Dipan.SanGuo.Tent.GetInfoResource();
    },
    
    /**
     * 营寨页面模板
     */
    Template:
    {
         BuildImages:["{for i in infos}",
                    "{if i[1] == 0}",
                    "",
                    "{elseif i[1] == 1}",
                    "<div class=\"tent${i[0]}\" id=\"tent${i[0]}\"><img src=\"${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/bulid/${i[1]}_${Dipan.SanGuo.Tent.GetBuildLevel(i[3])}.gif\" /></div>",
                    "{elseif i[1] == 2}",
                    "<div class=\"tent${i[0]}\" id=\"tent${i[0]}\"></div>",
                    "{elseif i[1] == 17}",
                    "<div class=\"tent${i[0]}\" id=\"tent${i[0]}\"></div>",
                    "{elseif i[1] > 0 && i[1] < 22}",
                    "<div class=\"tent${i[0]}\" id=\"tent${i[0]}\"><img src=\"${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/bulid/${i[1]}_${Dipan.SanGuo.Tent.GetBuildLevel(i[3])}.gif\" /></div>",
                    "{elseif i[1] > 21}",
                    "",
                    "{/if}",
                    "{/for}"],
        
        BuildAreas:["{for i in infos}",
                    '<area shape=\"poly\" onmouseover=\"Dipan.SanGuo.City.ShowBuildTips(${i[1]},${i[3]},${i[4]});\" onmouseout=\"Dipan.SanGuo.City.HideBuildTips();\" id=\"build${i[0]}\" coords=\"${Dipan.SanGuo.Tent.GetBuildAreas(i[0])}\" href=\"#\" onclick=\"Dipan.SanGuo.Tent.Build(${i[0]}, ${i[1]});return false;\" onfocus=\"this.blur()\" />',
                    "{/for}",
                    '<area shape=\"poly\" title=\"营寨栅栏\" id=\"build0\" coords=\"585,425,689,340,841,221,893,181,893,319,767,426,587,426\" href=\"#\" onclick=\"Dipan.SanGuo.Tent.Build(${infos[0][0]}, ${infos[0][1]});return false;\" onfocus=\"this.blur()\" />'],
                    
         CityInfo:['<div class="n info_att white_12b">',
                '<font class="black_12b">民心/民怨：</font><span title="民心">－</span> / <span title="民怨" class="orange_12b">－</span></span>',
                '</div>',
                '<div class="n info_att white_12b">',
                '<font class="black_12b">税收：</font><span title="税收">－</span></div>',
                '<div class="n info_att_w white_12b">',
                '<font class="black_12b">所属城池铜钱存量：</font><span title="城内铜钱存量" id="current_monkey">${money[0]}</span></span>',
                '</div>',
                '<div class="n info_att_w white_12b">',
                '<font class="black_12b">营寨内军事总人口：</font><span title="军事人口">${population[3]}</span></span>',
                '</div>'],
                
         Resource:['<div class="resource">',
'<ul>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'木材产量：${wood[1]}  ${Dipan.SanGuo.City.toChinese(wood[1])}<br />木材存量：${wood[0]}  ${Dipan.SanGuo.City.toChinese(wood[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==wood[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-wood[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-wood[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-wood[0]*1,wood[1],wood[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources01.gif" title="木材" />',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==wood[0]*1}orange_12b{elseif max_storage*0.8<=wood[0]*1}yellow3{else}yellow_8{/if}" id="current_wood">${wood[0]}</span></div>',
'<div class="{if wood[1]<=0}red{else}bright yellow8{/if}"><span id="output_wood">${wood[1]}</span>/h</div>',
'</b>',
'</li>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'铁矿产量：${iron[1]}  ${Dipan.SanGuo.City.toChinese(iron[1])}<br />铁矿存量：${iron[0]}  ${Dipan.SanGuo.City.toChinese(iron[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==iron[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-iron[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-iron[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-iron[0]*1,iron[1],iron[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources02.gif" title="铁矿" />',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==iron[0]*1}orange_12b{elseif max_storage*0.8<=iron[0]*1}yellow3{else}yellow_8{/if}" id="current_iron">${iron[0]}</span></div>',
'<div class="{if iron[1]<=0}red{else}bright yellow8{/if}"><span id="output_iron">${iron[1]}</span>/h</div>',
'</b>',
'</li>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'石料产量：${stone[1]}  ${Dipan.SanGuo.City.toChinese(stone[1])}<br />石料存量：${stone[0]}  ${Dipan.SanGuo.City.toChinese(stone[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==stone[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-stone[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-stone[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-stone[0]*1,stone[1],stone[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources03.gif" title="石料"/>',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==stone[0]*1}orange_12b{elseif max_storage*0.8<=stone[0]*1}yellow3{else}yellow_8{/if}" id="current_stone">${stone[0]}</span></div>',
'<div class="{if stone[1]<=0}red{else}bright yellow8{/if}"><span id="output_stone">${stone[1]}</span>/h</div>',
'</b>',
'</li>',
'<li onmouseover="Dipan.SanGuo.City.ShowItem(\'粮食消耗：<span class=red2>${food[2]}</span>  ${Dipan.SanGuo.City.toChinese(food[2])}<br />粮食产量：${food[1]}  ${Dipan.SanGuo.City.toChinese(food[1])}<br />粮食存量：${food[0]}  ${Dipan.SanGuo.City.toChinese(food[0])}<br />仓库最大存量：${max_storage}  ${Dipan.SanGuo.City.toChinese(max_storage)}<br />{if max_storage*1==food[0]*1}<span class=red2>已满仓</span>{else}还差 <span class=red2>${max_storage*1-food[0]*1}</span> 满仓  ${Dipan.SanGuo.City.toChinese(max_storage*1-food[0]*1)} ${Dipan.SanGuo.City.timeto(max_storage*1-food[0]*1,food[1],food[0])}{/if}\',1)" onmouseout="Dipan.SanGuo.Build.Fun.HideItem()">',
'<p>',
'<img src="${Dipan.SanGuo.Common.StaticsServer}/Statics/Images/resources04.gif" title="粮食"/>',
'</p>',
'<b>',
'<div class="bleft yellow_12">现：<span class="{if max_storage*1==food[0]*1}orange_12b{elseif max_storage*0.8<=food[0]*1}yellow3{else}yellow_8{/if}" id="current_food">${food[0]}</span></div>',
'<div class="{if food[1]<=0}red{else}bright yellow8{/if}"><span id="output_food">${food[1]}</span>/h</div>',
'</b>',
'</li>',
'</ul>',
'</div>']
    },
    /**
     * 获取营寨建筑
     */
    GetBuilding: function()
    {
        var RemoteData = new Dipan.SanGuo.Common.RemoteData()
        RemoteData.Post("/GateWay/City.ashx?id=5","tid=" + Dipan.SanGuo.Common.GetCookie("tid"));
        RemoteData.onComplete = function(resp)
        {//alert(resp);
            var JsonData = eval('(' + resp + ')');
            if (JsonData.ret * 1 == 0)
            {
                try
                {
                    Dipan.SanGuo.Common.__TimeOut(JsonData);
                    JsonData = Dipan.SanGuo.Tent.GetRealJsonData(JsonData);
                    Dipan.SanGuo.Common.FixData(Dipan.SanGuo.Tent.Template.BuildImages, "BuildImages", JsonData, true);
                    Dipan.SanGuo.Common.FixData(Dipan.SanGuo.Tent.Template.BuildAreas, "BuildAreas", JsonData, true);
                    //$("city_tent").className = "city tent_" + JsonData.size;
                    
                    $("city_tent").className = "city tent_" + Dipan.SanGuo.Tent.GetWallLevel(JsonData.infos[0]);
                    
                    if(!$('info_all'))return;
                    if($('info_all').style.display=="")Dipan.SanGuo.Common.GetInfoAll();//更新升级聚合信息
                }
                catch (e)
                {
                    //alert(e.message);
                }
            }
            else
            {
                Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
            }
        }
    },
    
    /**
     * 获取营寨栅栏登级
     * @param {info} 营寨栅栏信息
     */
    GetWallLevel: function(info)
    {
        if (info[2] == "营寨栅栏")
        {
            if (info[3] >= 0 && info[3] <= 10)
                return "1";
            else if (info[3] > 10 && info[3] <= 20)
                return "2";
            else if (info[3] > 20)
                return "3";
        }
        else
            return "1";
    },
    
    /**
     * 获取建筑登级
     * @param {info} 建筑信息
     */
    GetBuildLevel: function(size)
    {
        if (size >= 0 && size <= 10)
            return "1";
        else if (size > 10 && size <= 20)
            return "2";
        else if (size > 20)
            return "3";
        else
            return "0";
    },
    
    /**
     * 获取真实建筑信息
     * @param {JsonData} 已经有的建筑信息
     */
    GetRealJsonData: function(JsonData)
    {
        var data;
        
        data = "{ret:"+JsonData.ret+",size:"+JsonData.size+",infos:[";
        
        if (JsonData.infos.length > 0)
        {
            for (i = 0; i < 5; i++)
            {
                var flag = false;
                for (n = 0; n < JsonData.infos.length; n++)
                {
                    if (i == JsonData.infos[n][0])
                    {
                        flag = true;
                        data += "["+JsonData.infos[n][0]+","+JsonData.infos[n][1]+",\'"+JsonData.infos[n][2]+"\',"+JsonData.infos[n][3]+","+JsonData.infos[n][4]+"],";
                    }
                }
                if (!flag)
                {
                   data += "["+i+",0,\"空地\",1,0],";
                }
            }
        }
        else
        {
            for (i = 0; i < 5; i++)
            {
                data += "["+i+",0,\"空地\",1,0],";
            }
        }
        
        data += "]}";
        
        return eval('(' + data + ')');
    },
    
    /**
     * 获取营寨基本信息
     */
    GetCityInfo: function()
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        
        Loader.onsuccess = function()
        {
            var RemoteData = new Dipan.SanGuo.Common.RemoteData()
            RemoteData.Post("/GateWay/City.ashx?id=6","tid="+Dipan.SanGuo.Common.GetCookie("tid"));
            RemoteData.onComplete = function(resp)
            {
                var JsonData = eval('(' + resp + ')');
                
                if (JsonData.ret * 1 == 0)
                {
                    try
                    {
                        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.Tent.Template.CityInfo, "CityInfo", JsonData, true);
                    }
                    catch (e)
                    {
                        //alert(e.message);
                    }
                }
                else
                {
                     Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
                }
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/template.js", true);
    },
    
    /**
     * 获取营寨资源信息
     */
    GetInfoResource:function()
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        
        Loader.onsuccess = function()
        {
            var RemoteData = new Dipan.SanGuo.Common.RemoteData()
            RemoteData.Post("/GateWay/City.ashx?id=20","tid=" + Dipan.SanGuo.Common.GetCookie("tid"));
            RemoteData.onComplete = function(resp)
            {
                var JsonData = eval('(' + resp + ')');
                
                if (JsonData.ret * 1 == 0)
                {
                    try
                    {
                        Dipan.SanGuo.Common.FixData(Dipan.SanGuo.City.Template.Resource, "cityinfo_tab", JsonData, true);
                    }
                    catch (e)
                    {
                        //alert(e.message);
                    }
                }
                else
                {
                     Dipan.SanGuo.Common.ResultInfo(JsonData.ret);
                }
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/template.js", true);
    },
    
    /**
     * 获取建筑位置
     * @param {pos} 建筑位置ID
     */
    GetBuildAreas: function(pos)
    {
        return Dipan.SanGuo.Tent.BuildAreas[pos];
    },
    
    /**
     * 建筑位置配置信息
     */
    BuildAreas:
    [
        [217,4,386,5,158,180,149,188,176,232,300,304,381,395,419,424,354,426,230,427,9,273,5,181],
        [596,162,598,163,617,130,620,98,613,58,603,26,586,8,558,3,547,0,532,2,515,7,500,14,486,22,477,30,464,43,450,59,448,71,444,89,443,102,445,116,446,135,451,155,457,167,473,172,492,176,511,176,524,177,544,177,556,177,569,176,579,172,593,165],
        [432,260,434,261,453,228,456,196,449,156,439,124,422,106,394,101,383,98,368,100,351,105,336,112,322,120,313,128,300,141,286,157,284,169,280,187,279,200,281,214,282,233,287,253,293,265,309,270,328,274,347,274,360,275,380,275,392,275,405,274,415,270,429,263],
        [735,279,737,280,756,247,759,215,752,175,742,143,725,125,697,120,686,117,671,119,654,124,639,131,625,139,616,147,603,160,589,176,587,188,583,206,582,219,584,233,585,252,590,272,596,284,612,289,631,293,650,293,663,294,683,294,695,294,708,293,718,289,732,282],
        [576,397,578,398,597,365,600,333,593,293,583,261,566,243,538,238,527,235,512,237,495,242,480,249,466,257,457,265,444,278,430,294,428,306,424,324,423,337,425,351,426,370,431,390,437,402,453,407,472,411,491,411,504,412,524,412,536,412,549,411,559,407,573,400]
    ],
    
    /**
     * 根据建筑唯一ID显示建筑浮层
     * @param {pid} 建筑位置ID
     * @param {gid} 建筑唯一ID
     */
    Build: function(pid,gid,id,tab,cityId)
    {
        var Loader = new Dipan.SanGuo.Common.Loader();
        if(!id)id=0;
        if(!tab)tab=1;
        cityId=Dipan.SanGuo.Common.GetCookie("tid");
        Loader.onsuccess = function()
        {
            switch (gid)
            {
                case 0 ://空地
                    Dipan.SanGuo.Build.Fun.emptyLand(pid,gid,id,tab,cityId);
                    break;
                case 1 ://官府
                    Dipan.SanGuo.Build.Fun.government(pid,gid,id,tab,cityId);
                    break;
                case 2 ://城墙
                    Dipan.SanGuo.Build.Fun.wall(pid,gid,id,tab,cityId);
                    break;
                case 3 ://民房
                    Dipan.SanGuo.Build.Fun.house(pid,gid,id,tab,cityId);
                    break;
                case 4 ://仓库
                    Dipan.SanGuo.Build.Fun.campwarehouse(pid,gid,id,tab,cityId);
                    break;
                case 5 ://鸿胪寺

                    Dipan.SanGuo.Build.Fun.diplomacy(pid,gid,id,tab,cityId);
                    break;
                case 6 ://大学殿

                    Dipan.SanGuo.Build.Fun.academy(pid,gid,id,tab,cityId);
                    break;
                case 7 ://客栈
                    Dipan.SanGuo.Build.Fun.inn(pid,gid,id,tab,cityId);
                    break;
                case 8 ://酒馆
                    Dipan.SanGuo.Build.Fun.pub(pid,gid,id,tab,cityId);
                    break;
                case 9 ://聚贤楼

                    Dipan.SanGuo.Build.Fun.official(pid,gid,id,tab,cityId);
                    break;
                case 10 ://点将台

                    Dipan.SanGuo.Build.Fun.general(pid,gid,id,tab,cityId);
                    break;
                case 11 ://市场
                    Dipan.SanGuo.Build.Fun.market(pid,gid,id,tab,cityId);
                    break;
                case 12 ://驿站
                    Dipan.SanGuo.Build.Fun.posthouse(pid,gid,id,tab,cityId);
                    break;
                case 13 ://兵器铺

                    Dipan.SanGuo.Build.Fun.weapon(pid,gid,id,tab,cityId)
                    break;
                case 14 ://防具铺

                    Dipan.SanGuo.Build.Fun.shield(pid,gid,id,tab,cityId);
                    break;
                case 15 ://车马间

                    Dipan.SanGuo.Build.Fun.traffic(pid,gid,id,tab,cityId);
                    break;
                case 16 ://校场
                    Dipan.SanGuo.Build.Fun.barracks(pid,gid,id,tab,cityId);
                    break;
                case 17 ://营寨栅栏
                    Dipan.SanGuo.Build.Fun.wall(pid,gid,id,tab,cityId);
                    break;
                case 18 ://中军大帐
                    Dipan.SanGuo.Build.Fun.headquarters(pid,gid,id,tab,cityId);
                    break;
                case 19 ://营寨校场
                    Dipan.SanGuo.Build.Fun.barracks(pid,gid,id,tab,cityId);
                    break;
                case 20 ://营寨驿站
                    Dipan.SanGuo.Build.Fun.posthouse(pid,gid,id,tab,cityId);
                    break;
                case 21 ://营寨仓库
                    Dipan.SanGuo.Build.Fun.campwarehouse(pid,gid,id,tab,cityId);
                    break;
            }
        }
        
		Loader.js(Dipan.SanGuo.Common.StaticsServer + "/Statics/JavaScripts/Dipan.SanGuo.Config.js", true);
    },
    
    /**
     * 获取建筑名称
     * @param {gid}    建筑编号
     * @param {level}  建筑等级
     * @param {status} 建筑状态

     */
    BuildingName: function(gid, level, status)
    {
        var buildname;
        switch (gid)
        {
            case 0 ://空地
                buildname = "空地";
                break;
            case 1 ://官府
                buildname = "官府";
                break;
            case 2 ://城墙
                buildname = "城墙";
                break;
            case 3 ://民房
                buildname = "民房";
                break;
            case 4 ://仓库
                buildname = "仓库";
                break;
            case 5 ://鸿胪寺

                buildname = "鸿胪寺";
                break;
            case 6 ://大学殿

                buildname = "大学殿";
                break;
            case 7 ://客栈
                buildname = "客栈";
                break;
            case 8 ://酒馆
                buildname = "酒馆";
                break;
            case 9 ://聚贤楼

                buildname = "聚贤楼";
                break;
            case 10 ://点将台

                buildname = "点将台";
                break;
            case 11 ://市场
                buildname = "市场";
                break;
            case 12 ://驿站
                buildname = "驿站";
                break;
            case 13 ://兵器铺

                buildname = "兵器铺";
                break;
            case 14 ://防具铺

                buildname = "防具铺";
                break;
            case 15 ://车马间

                buildname = "车马间";
                break;
            case 16 ://校场
                buildname = "校场";
                break;
            case 17 ://营寨栅栏
                buildname = "营寨栅栏";
                break;
            case 18 ://中军大帐
                buildname = "中军大帐";
                break;
            case 19 ://营寨校场
                buildname = "营寨校场";
                break;
            case 20 ://营寨驿站
                buildname = "营寨驿站";
                break;
            case 21 ://营寨仓库
                buildname = "营寨仓库";
                break;
            default :
                buildname = "空地";
                break;
        }
        
        switch (status)
        {
            case 0 ://正常
                {
                    if (gid != 0)
                    {
                        buildname += " " + level + " 级";
                    }
                }
                break;
            case 1 ://升级中

                buildname += " 正在从 " + level + " 级升到 " + (level+1) + " 级";
                break;
            case 2 ://拆除中

                buildname += " 正在从 " + level + " 级拆除 " + (level-1) + " 级";
                break;
            case 3 ://顶级
                buildname += " " + level + " 级，已经到达顶级";
                break;
            default :
                buildname += " " + level + " 级";
                break;
        }
        
        return buildname;
    }
};